export const urlToObj = (url: string) => {
  const params: any = new URLSearchParams(url)
  return Object.fromEntries(params)
}

export const formatDate = (timestamp: string | number, showTime = false) => {
  const date = new Date(+timestamp)
  const month = (date.getMonth() + 1).toString().padStart(2, '0')
  const day = date.getDate().toString().padStart(2, '0')
  let str = `${date.getFullYear()}-${month}-${day}`
  if (showTime) {
    const hour = date.getHours().toString().padStart(2, '0')
    const minute = date.getMinutes().toString().padStart(2, '0')
    const second = date.getSeconds().toString().padStart(2, '0')
    str += ` ${hour}:${minute}:${second}`
  }
  return str
}

export const throttle = (fn: Function, delay: number = 300) => {
  let timer: any = null
  return function (...args: any[]) {
    if (timer) {
      return
    }
    timer = setTimeout(() => {
      fn.apply(this, args)
      timer = null
    }, delay)
  }
}
